﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace TicketTaking
{
    public class VBar
    {
        IntPtr dev;
        //打开设备dll
        [DllImport("libvbar.dll", EntryPoint = "vbar_open", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr vbar_open(IntPtr addr, long parm);
        //背光控制dll
        [DllImport("libvbar.dll", EntryPoint = "vbar_backlight", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool vbar_backlight(IntPtr dev, bool toset);
        //蜂鸣器控制dll
        [DllImport("libvbar.dll", EntryPoint = "vbar_beep", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool vbar_beep(IntPtr dev, int times);
        //判断设备是否已连接dll
        [DllImport("libvbar.dll", EntryPoint = "vbar_is_connected", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool vbar_is_connected(IntPtr dev);
        //添加要支持的码制dll
        [DllImport("libvbar.dll", EntryPoint = "vbar_add_symbol_type", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool vbar_add_symbol_type(IntPtr dev, int type);
        //扫码dll
        [DllImport("libvbar.dll", EntryPoint = "vbar_scan", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool vbar_scan(IntPtr dev, ref int type, [MarshalAs(UnmanagedType.LPArray)] byte[] result_buffer, ref int size);
        //关闭设备dll
        [DllImport("libvbar.dll", EntryPoint = "vbar_close", CallingConvention = CallingConvention.Cdecl)]
        public static extern void vbar_close(IntPtr dev);
        private static IntPtr mallocIntptr(string strData)
        {
            //先将字符串转化成字节方式  
            Byte[] btData = System.Text.Encoding.Default.GetBytes(strData);

            //申请非拖管空间  
            IntPtr m_ptr = Marshal.AllocHGlobal(btData.Length);

            //给非拖管空间清０   
            Byte[] btZero = new Byte[btData.Length + 1];
            Marshal.Copy(btZero, 0, m_ptr, btZero.Length);

            //给指针指向的空间赋值  
            Marshal.Copy(btData, 0, m_ptr, btData.Length);

            return m_ptr;
        }

        //打开设备
        public bool OpenDevice(string addr) //addr参数为连接设备所在电脑ip地址
        {
            dev = vbar_open(mallocIntptr(addr), 0);
            if (dev == IntPtr.Zero)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        //背光控制
        public bool Backlight(bool state)
        {
            if (vbar_backlight(dev, state))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //蜂鸣器控制
        public bool Beep(int times)
        {
            if (vbar_beep(dev, times))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //判断设备是否连接
        public bool IsConnected()
        {
            if (vbar_is_connected(dev))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //关闭设备
        public void CloseDevice()
        {
            vbar_close(dev);
        }
        //扫码
        public bool Scan(out byte[] result)
        {
            int c_type = 0;
            int c_size = 0;
            byte[] c_result = new byte[256];
            if (vbar_scan(dev, ref c_type, c_result, ref c_size))
            {
                result = c_result;
                return true;
            }
            else
            {
                result = null;
                return false;
            }
        }
        //码制类型
        public enum CodeType
        {
            VBAR_SYM_NONE = 0,        /* 空类型, 用于清空 */
            VBAR_SYM_QRCODE = 1,   //qrcode
            VBAR_SYM_EAN8 = 2,        //ean8
            VBAR_SYM_EAN13 = 3,     //ean13
            VBAR_SYM_ISBN13 = 4,   //isbn13
            VBAR_SYM_CODE39 = 5,   //code39
            VBAR_SYM_CODE93 = 6,   //code93
            VBAR_SYM_CODE128 = 7,  //code128
            VBAR_SYM_DATABAR = 8,  //databar
            VBAR_SYM_DATABAR_EXP = 9, //databar_exp
            VBAR_SYM_PDF417 = 10,  //pdf417
            VBAR_SYM_DATAMATRIX = 11,  //datamatrix
            VBAR_SYM_ITF = 12,   //itf
            VBAR_SYM_ISBN10 = 13,   //isbn10
            VBAR_SYM_UPCA = 15,   //upca
        }
        //添加要支持的码制
        public bool AddSymbolType(int type)
        {
            if (vbar_add_symbol_type(dev, type))
            {
                return true;
            }
            else
            {
                return false;
            }

        }
    }
}
